﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Model;

namespace TaxiDispatchClient
{
    public partial class AddDestinationForm : DevExpress.XtraEditors.XtraForm
    {
        public AddDestinationForm(double lng,double lat)
        {
            InitializeComponent();
            textEdit_Lng.Text = lng.ToString("f6");
            textEdit_Lat.Text = lat.ToString("f6");

        }

        public DestinationInfo DestInfo;

        private void btn_confirm_Click(object sender, EventArgs e)
        {
            if (!CheckInputValue())
            {
                return;
            }

            DestinationInfo model = new DestinationInfo();
            model.Name = textEdit_destName.Text;
            model.Lat = double.Parse(textEdit_Lat.Text);
            model.Lng = double.Parse(textEdit_Lng.Text);
            model.Type = Common.ConstParameters.CarTypeOfAmbulance;
         
            try
            {
                DestInfo = MainForm.PositionService.AddDestinationByClient(model);
                if (DestInfo != null)
                {                    
                    this.DialogResult = DialogResult.Yes;
                    XtraMessageBox.Show("添加成功！");
                    Close();
                }
                else
                {
                    this.DialogResult = DialogResult.No;
                    XtraMessageBox.Show("添加失败！");
                }
            }
            catch (Exception ex)
            {
                this.DialogResult = DialogResult.No;
                XtraMessageBox.Show("添加失败！");
            }


        }

        private void btn_cancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.No;
            Close();
        }

        private bool CheckInputValue()
        {
            if (textEdit_Lng.Text.Trim() == "")
            {
                XtraMessageBox.Show("伤员所在纬度不能为空！");
                return false;
            }
            if (textEdit_Lat.Text.Trim() == "")
            {
                XtraMessageBox.Show("伤员所在经度不能为空！");
                return false;
            }
            if (textEdit_destName.Text.Trim() == "")
            {
                XtraMessageBox.Show("地点名称不能为空！");
                return false;
            }
            return true;
        }

        private void textEdit_destName_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((Keys)e.KeyChar == Keys.Enter)
            {
                btn_confirm_Click(null, null);
            }
        }


        #region  窗体控件操作、效果
        Point _mouseOff;
        bool _leftFlag;
        private void Taxi_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                _mouseOff = new Point(-e.X, -e.Y);
                _leftFlag = true;
            }
        }

        private void Taxi_MouseMove(object sender, MouseEventArgs e)
        {
            if (_leftFlag)
            {
                Point mouseSet = Control.MousePosition;
                mouseSet.Offset(_mouseOff.X, _mouseOff.Y);
                Location = mouseSet;
            }
        }

        private void Taxi_MouseUp(object sender, MouseEventArgs e)
        {
            if (_leftFlag)
            {
                _leftFlag = false;
            }
        }

        private void pictureEdit_exit_MouseHover(object sender, EventArgs e)
        {
            pictureBox1.Image = Properties.Resources.snedSmsexit;
            Cursor = Cursors.Hand;
        }

        private void pictureEdit_exit_MouseLeave(object sender, EventArgs e)
        {
            pictureBox1.Image = Properties.Resources.snedSmsexitPressDown;

            Cursor = Cursors.Arrow;

        }

        private void pictureEdit_exit_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btn_MouseHover(object sender, EventArgs e)
        {
            Cursor = Cursors.Hand;
        }
        private void btn_MouseLeave(object sender, EventArgs e)
        {
            Cursor = Cursors.Default;
        }
        private void Form_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                Close();
            }
        }

        #endregion         
    }
}